﻿@namespace Aspire.Dashboard.Components
@using System.Globalization
@using Aspire.Dashboard.Otlp.Model
@using Aspire.Dashboard.Resources
@using Aspire.Dashboard.Utils
@using Aspire.Hosting.ConsoleLogs

@inject IJSRuntime JS
@inject IStringLocalizer<ConsoleLogs> Loc

@implements IAsyncDisposable

<div class="log-overflow console-overflow continuous-scroll-overflow">
    <div class="@GetLogContainerClass()" id="logContainer">
        @if (LogEntries is { } logEntries)
        {
            <Virtualize Items="@logEntries.GetEntries()" ItemSize="20" OverscanCount="200" TItem="LogEntry">
                @if (context.Pause is { } pause)
                {
                    // If this is a previous pause but no logs were obtained during the pause, we don't need to show anything.
                    if (pause is { FilteredCount: 0, EndTime: not null })
                    {
                        return;
                    }

                    <div class="log-line-row-container">
                        <div class="log-line-row console-line-row">
                            <div class="log-line-area" role="log">
                                <span class="log-line-number"></span>
                                <span class="log-content log-pause">
                                    @if (ShowResourcePrefix && context.ResourcePrefix is { } resourcePrefix)
                                    {
                                        <span class="resource-prefix" style="background: @ColorGenerator.Instance.GetColorHexByKey(resourcePrefix);">@resourcePrefix</span>
                                    }
                                    @pause.GetDisplayText(Loc, TimeProvider)
                                </span>
                            </div>
                        </div>
                    </div>
                }
                else
                {
                     <div class="log-line-row-container">
                        <div class="log-line-row console-line-row">
                            <span class="log-line-area" role="log">
                                <span class="log-line-number">@context.LineNumber</span>
                                <span class="log-content">
                                    @{
                                        var hasPrefix = false;
                                    }
                                    @if (ShowTimestamp && context.Timestamp is { } timestamp)
                                    {
                                        if (DisplayPrefix(ref hasPrefix))
                                        {
                                            @s_spaceMarkup
                                        }
                                        <span class="timestamp" title="@FormatHelpers.FormatDateTime(TimeProvider, timestamp, MillisecondsDisplay.Full, CultureInfo.CurrentCulture)">@GetDisplayTimestamp(timestamp)</span>
                                    }
                                    @if (ShowResourcePrefix && context.ResourcePrefix is { } resourcePrefix)
                                    {
                                        if (DisplayPrefix(ref hasPrefix))
                                        {
                                            @s_spaceMarkup
                                        }
                                        <span class="resource-prefix" style="background: @ColorGenerator.Instance.GetColorHexByKey(resourcePrefix);">@resourcePrefix</span>
                                    }
                                    @if (context.Type == LogEntryType.Error)
                                    {
                                        if (DisplayPrefix(ref hasPrefix))
                                        {
                                            @s_spaceMarkup
                                        }
                                        <fluent-badge appearance="accent">stderr</fluent-badge>
                                    }
                                    @if (DisplayPrefix(ref hasPrefix))
                                    {
                                        @s_spaceMarkup
                                    }
                                    @((MarkupString)(context.Content ?? string.Empty))
                                </span>
                            </span>
                        </div>
                    </div>
                }
            </Virtualize>
        }
    </div>
</div>

@code {
    public bool DisplayPrefix(ref bool hasPrefix)
    {
        if (hasPrefix)
        {
            return true;
        }

        hasPrefix = true;
        return false;
    }
}
